package cn.tianjindong.blog_manager.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.tianjindong.blog.common.pojo.PageView;
import cn.tianjindong.blog.pojo.BlogMessageBoard;
import cn.tianjindong.blog_manager.service.MessageService;

@Controller
public class MessageBoardController {
	
	@Autowired
	private MessageService messageService;

	/**
	 * 前往“留言列表”页面
	 * @param model
	 * @param currentPage
	 * @param countItem
	 * @return
	 */
	@RequestMapping("/message/list.html")
	public String toMessageBoardListPage(Model model, @RequestParam(defaultValue = "1") Integer currentPage,
			@RequestParam(defaultValue = "15") Integer countItem) {
		PageView<BlogMessageBoard> pageView = messageService.getMessagePageView(currentPage, countItem);
		model.addAttribute("pageView", pageView);
		return "/message-list";
	}
	
	/**
	 * 前往留言详情页面
	 * @param model
	 * @param messageId
	 * @return
	 */
	@RequestMapping("/message/{messageId}.html")
	public String toMessageBoardPage(Model model,@PathVariable Long messageId){
		BlogMessageBoard message = messageService.getMessageById(messageId);
		model.addAttribute("message", message);
		return "/message";
	}
	
	/**
	 * 删除指定留言
	 * @return
	 */
	@RequestMapping("/message/delete.action")
	public @ResponseBody String deleteMessage(Long messageId){
		messageService.deleteById(messageId);
		return "{\"status\":\"ok\"}";
	}
	
	/**
	 * 删除指定留言
	 * @return
	 */
	@RequestMapping("/message/reverse-tagreply.action")
	public @ResponseBody String reverseTagReply(Long messageId){
		messageService.reverseTagReply(messageId);
		return "{\"status\":\"ok\"}";
	}
	
}
